package com.sicnu.startup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sicnu.startup.entity.Users;
import com.sicnu.startup.exception.StartupException;
import com.sicnu.startup.mapper.UsersMapper;
import com.sicnu.startup.result.ResultCodeEnum;
import com.sicnu.startup.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sicnu.startup.util.CodeUtil;
import com.sicnu.startup.util.MD5;
import com.sicnu.startup.vo.LoginVO;
import com.sicnu.startup.vo.RegisterVO;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zr
 * @Author yanggang
 * @Description: 服务实现类
 * @Data Created in 11:26 2022/1/6
 * @since 2022-01-06
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 注册用户
     *
     * @param registerVO
     * @return
     */
    @Override
    public Boolean registerUser(RegisterVO registerVO, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        // 获取用户信息
        String nickname = registerVO.getNickname();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();
        // 进行判空
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code)) {
            throw new StartupException("不能为空", 222);
        }
        // 从redis中取出验证码并判断
        String phoneCode = (String) servletContext.getAttribute("phone");
        // 判断验证码是否正确
        if (!Objects.equals(phoneCode, code)) {
            throw new StartupException("您输入的验证码不正确", 20001);
        }
        // 查询用户昵称  是否重复
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new StartupException(ResultCodeEnum.DATA_ERROR);
        }
        Users users = new Users();
        users.setMobile(mobile);
        users.setPassword(MD5.encrypt(password));
        users.setNickname(nickname);
        users.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/WcNZdTpzxFkjfVvbElWBpPrak6gx0amrmOPhYjTsVgF3ENdz7XjAkVMlQM2YFnibSzInD1PhejBiczLIr2f4yhHQ/132");
        users.setGmtModified(new Date());
        int insert = baseMapper.insert(users);
        return insert > 0;
    }


    @Override
    public Users login(LoginVO loginVO, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        String nickname = loginVO.getNickname();
        String password = loginVO.getPassword();
        String code = loginVO.getCode();
        // 从redis中取出验证码并判断
        String imageCode = (String) servletContext.getAttribute("imageCode");
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {
            throw new StartupException(ResultCodeEnum.DATA_ERROR);
        }
        if (!Objects.equals(imageCode, code)) {
            throw new StartupException(ResultCodeEnum.DATA_ERROR);
        }
        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<>();
        usersQueryWrapper.eq("nickname", nickname);
        Users users = baseMapper.selectOne(usersQueryWrapper);
        if (users == null) {
            throw new StartupException(ResultCodeEnum.DATA_ERROR);
        }
        if (!Objects.equals(MD5.encrypt(password), users.getPassword())) {
            throw new StartupException(ResultCodeEnum.DATA_ERROR);
        }

        return users;
    }

}
